<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Goods_model short summary.
 *
 * Goods_model description.
 *
 * @version 1.0
 * @author xianbin
 */
class Goods_model extends CI_Model
{

    public $goods_id;
    public $title;
    public $imgurl;
    public $goods_desc;
    public $shop_price;
    public $market_price;
    public $is_on_sale;
    public $is_new;
    public $is_hot;
    public $seo_keyword;
    public $seo_description;
    public $weight;
    public $addtime;
    public $good_material_id;
    public $goods_style_id;
    public $style;

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    /**
     * 添加商品
     * @param mixed $goodsModel 商品模型对象
     * @return mixed
     */
    public function insert_goods($goodsModel)
    {
        return $this->db->insert('goods',$goodsModel);
    }

    /**
     * 根据商品id获取商品对象
     * @param mixed $goodsid 商品id
     * @return mixed
     */
    public function get_goods_by_id($goodsid)
    {
        return $this->db->select()->from('goods')->where(array('goods_id'=>$goodsid))->get()->row();
    }

    /**
     * 更新商品
     * @param mixed $goodsModel 商品模型对象
     * @return mixed
     */
    public function edit_goods($goodsModel)
    {
        return $this->db->update('goods',$goodsModel,array('goods_id'=>$goodsModel->goods_id));
    }

    /**
     * 删除商品-放进回收站
     * @param mixed $goodsid 商品id
     * @return mixed
     */
    public function del_goods($goodsid)
    {
        $sql="update goods set is_del=1 where goods_id=$goodsid";
        return $this->db->query($sql);
    }

    /**
     * 批量删除商品-放入回收站
     * @param mixed $goods_ids 商品id数组集合
     * @return int
     */
    public function batchdel_goods($goods_ids)
    {
        $this->db->trans_begin();
        foreach($goods_ids as $goodsiditem)
        {
            $this->del_goods($goodsiditem);
        }
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }


    /**
     * 重新启用放入回收站的商品
     * @param mixed $goodsid 商品id
     * @return mixed
     */
    public function enable_goods($goodsid)
    {
        $sql="update goods set is_del=0 where goods_id=$goodsid";
        return $this->db->query($sql);
    }
    /**
     * 批量重新启用放入回收站的商品
     * @param mixed $goods_ids 商品id 数组集合
     * @return int
     */
    public function batchenable_goods($goods_ids)
    {
        $this->db->trans_begin();
        foreach($goods_ids as $goodsiditem)
        {
            $this->enable_goods($goodsiditem);
        }
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }

    public function get_page($cpage,$pagesize,$files,$tabel,$order,$temp_id,$where,$temStr)
    {
        $sql="SELECT $files FROM $tabel a JOIN (select $temp_id from $tabel $where $order limit ".($cpage-1)*$pagesize.", $pagesize) b ON a.$temp_id = b.$temp_id $temStr";
        $sql_count="select count('$temp_id') as _count from $tabel $where";
        $data['objlist']=$this->db->query($sql)->result();
        $data['count']=$this->db->query($sql_count)->result();
        return $data;
    }


    //前段调用
    public function get_discount_goods_limit_tow()
    {
        $sql="select * from goods where is_del=0 and is_promotion=1 and is_on_sale=1 order by weight desc,goods_id desc limit 2";
        return $this->db->query($sql)->result();
    }

    public function get_discount_goods_limit_six()
    {
        $sql="select * from goods where is_del=0 and is_hot=1 and is_on_sale=1 order by weight desc,goods_id desc limit 6";
        return $this->db->query($sql)->result();
    }
    public function get_hot_goods_()
    {
        $sql="select * from goods where is_del=0 and is_hot=1 and is_on_sale=1 order by weight desc,goods_id desc";
        return $this->db->query($sql)->result();
    }
}